﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.SessionState;
using Omes.Model;
using Omes.Services;

namespace FashionMural.Admin
{
    public class jsserver : IHttpHandler, IRequiresSessionState
    {
        #region IHttpHandler Members

        public bool IsReusable
        {
            // Return false in case your Managed Handler cannot be reused for another request.
            // Usually this would be false in case you have some state information preserved per request.
            get { return false; }
        }

        public void ProcessRequest(HttpContext context)
        {
            string meth = context.Request.HttpMethod;
            switch (meth)
            {
                case "GET":
                    {
                        this.Get(context);
                    } break;
                case "POST":
                    {
                        this.Post(context);
                    }
                    break;
            }
        }

        private void Get(HttpContext context)
        {
            string result = string.Empty;
            string path = context.Request.Path.ToLower();
            //去掉path在web.config中的部分
            int index = path.IndexOf('/', 1);
            path = path.Substring(index);
            switch (path)
            {
                case "/page/delete.aspx":
                    {
                        int id = Convert.ToInt32(context.Request.QueryString["id"]);
                        DbServer db1 = new DbServer();
                        Omes.Model.Page page = db1.GetPage(id);
                        if (page != null)
                        {
                            try
                            {
                                string spath = context.Server.MapPath(page.SmallPhotoPath);
                                System.IO.File.Delete(spath);
                                UploadFileManage.DeleteNail(spath);
                            }
                            catch (Exception)
                            {

                            }
                            DbServer db = new DbServer();
                            result = db.DeletePage(id).ToString();
                        }
                    } break;
                case "/html/delete.aspx":
                    {
                        int id = Convert.ToInt32(context.Request.QueryString["id"]);
                        DbServer db = new DbServer();
                        result = db.DeleteHtml(id).ToString();
                    } break;
                case "/news/delete.aspx":
                    {
                        int id = Convert.ToInt32(context.Request.QueryString["id"]);
                        DbServer db = new DbServer();
                        result = db.DeleteNews(id).ToString();
                    } break;
                case "/all/menutype.aspx":
                    {
                        Omes.Services.DbServer db = new DbServer();
                        List<PageType> pageTypes = db.GetAllPageTypes();
                        result = Serializer.JsonSerializer<List<PageType>>(pageTypes);
                    } break;
                case "/all/parentpage.aspx":
                    {
                        Omes.Services.DbServer db = new DbServer();
                        List<Page> pages = db.GetParentPages();
                        result = Serializer.JsonSerializer<List<Page>>(pages);
                    } break;
                case "/page/childs.aspx":
                    {
                        int id = Convert.ToInt32(context.Request.QueryString["parentId"]);
                        Omes.Services.DbServer db = new DbServer();
                        List<Omes.Model.Page> temp = db.UseParentIdGetPages(id);
                        result = Serializer.JsonSerializer<List<Page>>(temp);
                    } break;
                case "/page/html.aspx":
                    {
                        int id = Convert.ToInt32(context.Request.QueryString["id"]);
                        Omes.Services.DbServer db = new DbServer();
                        Html html = db.GetPageHtml(id);
                        result = Serializer.JsonSerializer<Omes.Model.Html>(html);
                    } break;
                case "/page/news.aspx":
                    {
                        int id = Convert.ToInt32(context.Request.QueryString["id"]);
                        DbServer db = new DbServer();
                        List<NewsList> news = db.GetPageNews(id);
                        result = Serializer.JsonSerializer<List<NewsList>>(news);
                    } break;
                case "/news/sigle.aspx":
                    {
                        int id = Convert.ToInt32(context.Request.QueryString["id"]);
                        DbServer db = new DbServer();
                        NewsList news = db.GetNewsInfo(id);
                        result = Serializer.JsonSerializer<NewsList>(news);
                    } break;
                case "/page/images.aspx":
                    {
                        int id = Convert.ToInt32(context.Request.QueryString["pageId"]);
                        DbServer db = new DbServer();
                        List<ImageList> images = db.GetImageListOfPage(id);
                        result = Serializer.JsonSerializer<List<ImageList>>(images);
                    } break;
                case "/image/sigle.aspx":
                    {
                        int id = Convert.ToInt32(context.Request.QueryString["id"]);
                        DbServer db = new DbServer();
                        ImageList temp = db.GetImage(id);
                        result = Serializer.JsonSerializer<ImageList>(temp);
                    } break;
                case "/iamge/delete.aspx":
                    {
                        int id = Convert.ToInt32(context.Request.QueryString["id"]);
                        DbServer db1 = new DbServer();
                        ImageList image = db1.GetImage(id);
                        try
                        {
                            string ipath = context.Server.MapPath(image.Path);
                            System.IO.File.Delete(ipath);
                            UploadFileManage.DeleteNail(ipath);
                        }
                        catch (Exception)
                        {

                            throw;
                        }
                        DbServer db = new DbServer();
                        result = db.DeleteImage(id).ToString();
                    } break;
                case "/content/count.aspx":
                    {
                        string tableName = context.Request.QueryString["who"];
                        DbServer db = new DbServer();
                        result = db.GetContentCount(tableName).ToString();
                    } break;
                case "/content/data.aspx":
                    {
                        string tableName = context.Request.QueryString["who"];
                        int mindex = Convert.ToInt32(context.Request.QueryString["index"]);
                        int count = Convert.ToInt32(context.Request.QueryString["count"]);
                        DbServer db = new DbServer();
                        switch (tableName.ToLower())
                        {
                            case "leavemessage":
                                {
                                    List<LeaveMessage> temp = db.GetLeaveMessages(mindex, count);
                                    result = Serializer.JsonSerializer<List<LeaveMessage>>(temp);
                                } break;
                            case "joinusmessage":
                                {
                                    List<JoinUsMessage> temp = db.GetJoinUsMessges(mindex, count);
                                    result = Serializer.JsonSerializer<List<JoinUsMessage>>(temp);
                                } break;
                            case "friendlink":
                                {
                                    List<FriendLink> temp = db.GetFriendLinks(mindex, count);
                                    result = Serializer.JsonSerializer<List<FriendLink>>(temp);
                                } break;
                        }
                    } break;
                case "/content/delete.aspx":
                    {
                        string tableName = context.Request.QueryString["who"];
                        int id = Convert.ToInt32(context.Request.QueryString["id"]);
                        DbServer db = new DbServer();
                        switch (tableName.ToLower())
                        {
                            case "leavemessage":
                                {
                                    result = db.DeleteLeaveMessge(id).ToString();
                                } break;
                            case "joinusmessage":
                                {
                                    result = db.DeleteJoinUsMessage(id).ToString();
                                } break;
                            case "friendlink":
                                {
                                    result = db.DeleteFriendLink(id).ToString();
                                } break;
                        }
                    } break;
                case "/admin/info.aspx":
                    {
                        try
                        {
                            string username = context.Session["admin"].ToString();
                            DbServer db = new DbServer();
                            List<AdminInfo> admins = db.GetAdminInfo(username);
                            result = Serializer.JsonSerializer<AdminInfo>(admins[0]);
                        }
                        catch (Exception)
                        {

                            throw;
                        }
                    } break;
                case "/admin/all.aspx":
                    {
                        try
                        {
                            DbServer db = new DbServer();
                            List<AdminInfo> admins = db.GetAllAdminInfos();
                            result = Serializer.JsonSerializer<List<AdminInfo>>(admins);
                        }
                        catch (Exception)
                        {

                            throw;
                        }
                    } break;
                case "/loginrecord/separate.aspx":
                    {
                        int num = Convert.ToInt32(context.Request.QueryString["pageNumber"]);
                        int sindex = (num - 1) * 10;
                        if (sindex >= 0)
                        {
                            DbServer db = new DbServer();
                            List<LoginRecord> temps = db.GetLoginRecords(sindex, 10);
                            result = Serializer.JsonSerializer<List<LoginRecord>>(temps);
                        }
                    } break;
                case "/loginrecord/count.aspx":
                    {
                        DbServer db = new DbServer();
                        result = db.GetLoginRecordCount().ToString();
                    } break;
                case "/admin/username.aspx":
                    {
                        int userid = Convert.ToInt32(context.Request.QueryString["adminid"]);
                        DbServer db = new DbServer();
                        AdminInfo adminInfo = db.GetAdminInfo(userid);
                        result = adminInfo.AdminName;
                    } break;
                case "/admininfo/delete.aspx":
                    {
                        int userId = Convert.ToInt32(context.Request.QueryString["userid"]);
                        DbServer db = new DbServer();
                        result = db.DeleteAdminInfo(userId).ToString();
                    } break;
                case "/loginrecord/delete.aspx":
                    {
                        int id = Convert.ToInt32(context.Request.QueryString["id"]);
                        DbServer db = new DbServer();
                        result = db.DeleteLoginRecord(id).ToString();
                    } break;
                case "/questionnaire/users.aspx":
                    {
                        int qindex = Convert.ToInt32(context.Request.QueryString["qindex"]);
                        int qcount = Convert.ToInt32(context.Request.QueryString["qcount"]);
                        DbServer db = new DbServer();
                        result = Serializer.JsonSerializer<List<Anonymous>>(db.GetQuestionNaireUsers(qindex, qcount));
                    } break;
                case "/questionnaire/users/count.aspx":
                    {
                        DbServer db = new DbServer();
                        result = db.GetQuestionNaireUsersCount().ToString();
                    } break;
                case "/questionnaire/delete.aspx":
                    {
                        int id = Convert.ToInt32(context.Request.QueryString["qid"]);
                        DbServer db = new DbServer();
                        bool one = db.DeleteAnonymous(id);
                        DbServer db1 = new DbServer();
                        bool two = db1.DeleteAnonymousUserQuestionNaire(id);
                        if (one && two)
                        {
                            result = "True";
                        }
                    } break;
                case "/question/all.aspx":
                    {
                        DbServer db = new DbServer();
                        result = Serializer.JsonSerializer<List<Omes.Model.QuestionNaire>>(db.GetAllQuestionNaires());
                    } break;
                case "/question/all/count.aspx":
                    {
                        DbServer db = new DbServer();
                        result = db.GetAllQuestionCount().ToString();
                    }break;
                case "/question/separete.aspx":
                    {
                        int sindex = Convert.ToInt32(context.Request.QueryString["sindex"]);
                        int scount = Convert.ToInt32(context.Request.QueryString["scount"]);
                        DbServer db = new DbServer();
                        result =
                            Serializer.JsonSerializer<List<Omes.Model.QuestionNaire>>(db.GetQuestions(sindex, scount));
                    }break;
                case "/question/answer.aspx":
                    {
                        int userid = Convert.ToInt32(context.Request.QueryString["userid"]);
                        int questionid = Convert.ToInt32(context.Request.QueryString["questionid"]);
                        DbServer db = new DbServer();
                        result = Serializer.JsonSerializer<List<string>>(db.GetUserQuestionsAnswers(userid, questionid));
                    }break;
                case "/quesiton/anonyquestionanswer.aspx":
                    {
                        int userid = Convert.ToInt32(context.Request.QueryString["userid"]);
                        DbServer db = new DbServer();
                        List<AnonyQuestionQuestionAnswer> temp = db.GetQuestionNairesUseUserId(userid);
                        result = Serializer.JsonSerializer<List<AnonyQuestionQuestionAnswer>>(temp);
                    } break;
                case "/user/question/answer.aspx":
                    {
                        int qid = Convert.ToInt32(context.Request.QueryString["qid"]);
                        int userid = Convert.ToInt32(context.Request.QueryString["userid"]);
                        DbServer db = new DbServer();
                        List<Omes.Model.QuestionNaireAnswer> temp = db.GetQuestionNarieAnswerUseQuestionId(qid);
                        result = Serializer.JsonSerializer<List<QuestionNaireAnswer>>(temp);
                    } break;
            }
            context.Response.ContentEncoding = System.Text.Encoding.UTF8;
            context.Response.Write(result);
        }

        private void Post(HttpContext context)
        {
            string result = string.Empty;
            string path = context.Request.Path.ToLower();
            //去掉path在web.config中的部分
            int index = path.IndexOf('/', 1);
            path = path.Substring(index);
            switch (path)
            {
                case "/page/add.aspx":
                    {
                        string title = context.Request.Params["title"];
                        int parentId = context.Request.Params["parentId"] == "" ? -1 : Convert.ToInt32(context.Request.Params["parentId"]);
                        int pageTypeId = context.Request.Params["pageTypeId"] == "" ? -1 : Convert.ToInt32(context.Request.Params["pageTypeId"]);
                        string pageDescribe = context.Request.Params["pagedes"];
                        string ppath = context.Request.Params["phopath"];
                        string phocontent = context.Request.Params["phocontent"];
                        DbServer db = new DbServer();
                        result = db.AddPage(title, parentId, pageTypeId, pageDescribe, ppath, phocontent).ToString();
                    } break;
                case "/page/update.aspx":
                    {
                        int id = Convert.ToInt32(context.Request.Params["id"]);
                        string name = context.Request.Params["name"];
                        int patype = Convert.ToInt32(context.Request.Params["pagetype"]);
                        string pasmallpath = context.Request.Params["smallphopath"];
                        string padescri = context.Request.Params["pagedescribe"];
                        string phocontent = context.Request.Params["phocontent"];
                        try
                        {
                            DbServer db1 = new DbServer();
                            string temppath = db1.GetPage(id).SmallPhotoPath;
                            if (temppath != pasmallpath)
                            {
                                string prephopath = context.Server.MapPath(temppath);
                                System.IO.File.Delete(prephopath);
                            }
                        }
                        catch (Exception)
                        {

                        }
                        DbServer db = new DbServer();
                        result = db.UpdatePageNew(id, patype, name, pasmallpath, padescri, phocontent).ToString();
                    } break;
                case "/check/user.aspx":
                    {
                        string username = context.Request.Params["username"];
                        string password = context.Request.Params["password"];
                        DbServer db = new DbServer();
                        bool valide = db.CheckUser(username, password);
                        result = valide.ToString();
                        context.Session["admin"] = username;
                        string loginip = context.Request.UserHostAddress;
                        DbServer db1 = new DbServer();
                        AdminInfo adminInfo = db1.GetAdminInfo(username)[0];
                        DbServer db2 = new DbServer();
                        db2.AddLoinRecord(adminInfo.Id, loginip, DateTime.Now);
                        //System.Web.Security.FormsAuthentication.SetAuthCookie(username,false);
                    } break;
                case "/html/update.aspx":
                    {
                        string title = context.Request.Params["title"];
                        string content = context.Request.Params["content"];
                        int id = context.Request.Params["belongId"] == "" ? -1 : Convert.ToInt32(context.Request.Params["belongId"]);
                        string commend = context.Request.Params["commend"];
                        int weight = context.Request.Params["weight"] == "" ? -1 : Convert.ToInt32(context.Request.Params["weight"]);
                        string describe = context.Request.Params["describe"];
                        string blockName = context.Request.Params["blockname"];
                        DbServer db = new DbServer();
                        result = db.UpdateHtml(id, title, content, commend, weight, describe, blockName).ToString();
                    } break;
                case "/news/update.aspx":
                    {
                        string title = context.Request.Params["title"];
                        string content = context.Request.Params["content"];
                        int id = context.Request.Params["id"] == "" ? -1 : Convert.ToInt32(context.Request.Params["id"]);
                        string commend = context.Request.Params["commend"];
                        int weight = context.Request.Params["weight"] == "" ? -1 : Convert.ToInt32(context.Request.Params["weight"]);
                        string describe = context.Request.Params["describe"];
                        string blockName = context.Request.Params["blockname"];
                        DbServer db = new DbServer();
                        result = db.UpdateNews(id, title, content, commend, weight, describe, blockName).ToString();
                    } break;
                case "/news/add.aspx":
                    {
                        string title = context.Request.Params["title"];
                        string content = context.Request.Params["content"];
                        int belongId = context.Request.Params["belongid"] == "" ? -1 : Convert.ToInt32(context.Request.Params["belongid"]);
                        string commend = context.Request.Params["commend"];
                        int weight = context.Request.Params["weight"] == "" ? -1 : Convert.ToInt32(context.Request.Params["weight"]);
                        string describe = context.Request.Params["describe"];
                        string blockName = context.Request.Params["blockname"];
                        DbServer db = new DbServer();
                        result = db.AddNews(title, content, commend, weight, describe, blockName, belongId).ToString();
                    } break;
                case "/image/add.aspx":
                    {
                        string title = context.Request.Params["title"];
                        string ipath = context.Request.Params["path"];
                        int belongId = context.Request.Params["belongid"] == "" ? -1 : Convert.ToInt32(context.Request.Params["belongid"]);
                        string commend = context.Request.Params["commend"];
                        int weight = context.Request.Params["weight"] == "" ? -1 : Convert.ToInt32(context.Request.Params["weight"]);
                        string describe = context.Request.Params["describe"];
                        string blockName = context.Request.Params["blockname"];
                        DbServer db = new DbServer();
                        result = db.AddImage(title, ipath, commend, weight, describe, blockName, belongId).ToString();
                    } break;
                case "/image/update.aspx":
                    {
                        string title = context.Request.Params["title"];
                        string ipath = context.Request.Params["path"];
                        int id = context.Request.Params["id"] == "" ? -1 : Convert.ToInt32(context.Request.Params["id"]);
                        string commend = context.Request.Params["commend"];
                        int weight = context.Request.Params["weight"] == "" ? -1 : Convert.ToInt32(context.Request.Params["weight"]);
                        string describe = context.Request.Params["describe"];
                        string blockName = context.Request.Params["blockname"];
                        DbServer db = new DbServer();
                        result = db.UpdateImage(title, ipath, commend, weight, describe, blockName, id).ToString();
                    } break;
                case "/friendlink/update.aspx":
                    {
                        int id = Convert.ToInt32(context.Request.Params["id"]);
                        string title = context.Request.Params["title"];
                        string furl = context.Request.Params["furl"];
                        DbServer db = new DbServer();
                        result = db.UpdateFriendLink(id, title, furl).ToString();
                    } break;
                case "/friendlink/add.aspx":
                    {
                        string title = context.Request.Params["title"];
                        string furl = context.Request.Params["furl"];
                        DbServer db = new DbServer();
                        result = db.AddFriendLink(title, furl).ToString();
                    } break;
                case "/admininfo/update.aspx":
                    {
                        string adminname = context.Request.Params["adminname"];
                        string password = context.Request.Params["oldpassword"];
                        string newpassword = context.Request.Params["newpassword"];
                        string displayname = context.Request.Params["displayname"];
                        DbServer db1 = new DbServer();
                        bool valide = db1.CheckUser(adminname, password);
                        if (valide)
                        {
                            DbServer db2 = new DbServer();
                            result = db2.UpdateAdminInfo(adminname, displayname, newpassword).ToString();
                        }
                    } break;
                case "/admininfo/update1.aspx":
                    {
                        int id = Convert.ToInt32(context.Request.Params["id"]);
                        string adminName = context.Request.Params["adminname"];
                        string displayname = context.Request.Params["displayname"];
                        string password = context.Request.Params["password"];
                        DbServer db = new DbServer();
                        result = db.TrueUpdateAdminInfo(id, adminName, displayname, password).ToString();
                    } break;
                case "/admininfo/add.aspx":
                    {
                        string adminName = context.Request.Params["adminname"];
                        string displayname = context.Request.Params["displayname"];
                        string password = context.Request.Params["password"];
                        DbServer db = new DbServer();
                        result = db.AddAdminInfo(adminName, displayname, password).ToString();
                    } break;
            }
            context.Response.ContentEncoding = System.Text.Encoding.UTF8;
            context.Response.Write(result);
        }
        #endregion
    }
}
